package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CarModelDao;
import com.cxh.dao.CarModelOthernameDao;
import com.cxh.entity.CarModel;
import com.cxh.entity.CarModelOthername;
import com.cxh.service.CarModelService;
import com.cxh.util.CommonUtility;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 类说明：学生表 
 * @author  YangHL
 * @date  2018-10-15 14:06:57
 */
@Service("carModelService")
@DataSource("dataSource2")
public class CarModelServiceImpl extends Data2ServiceImpl<CarModel> implements CarModelService {
    @Autowired
    private CarModelDao dao;
    @Autowired
    private CarModelOthernameDao carModelOthernameDao;


    @Override
    public List<CarModel> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }
    
    @Override
    public List<CarModel> getPriceData(CarModel carModel2, HttpServletRequest request) {
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/CarManage/";
    	basePath = basePath+"common/img/";
    	carModel2.setImg(basePath);
    	return dao.getPriceData(carModel2);
    }
    
    @Override
    public List<CarModel> getPriceDataByYear(CarModel carModel, HttpServletRequest request) {
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/CarManage/";
    	basePath = basePath+"common/img/";
    	carModel.setImg(basePath);
    	return dao.getPriceDataByYear(carModel);
    }
    
    @Override
    public List<String> getGearbox(CarModel carModel) {
    	return dao.getGearbox(carModel);
    }
    
    @Override
    public List<String> getModel(CarModel carModel) {
    	return dao.getModel(carModel);
    }
    
    @Override
    public Map<String, Object> getPriceBetween(HttpServletRequest request,String carCategoryId) {
    	String contextPath = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath;
    	basePath = basePath+"/common/img/";
    	CarModel carModel = new CarModel();
    	carModel.setImg(basePath);
    	carModel.setCarcategoryid(carCategoryId);
    	return dao.getPriceBetween(carModel);
    }
    
    @Override
    public List<CarModel> loadPageListPage(Map<String, Object> params) {
    	return dao.loadPageListPage(params);
    }
    
    @Override
    public void deleteAboutData(CarModel carModel) {
    	dao.deleteAboutData(carModel.getCarmodelid());
    }
    
    @Override
    public void updateData(CarModel carModelSave, String otherName) {
    	dao.updateByPrimaryKey(carModelSave);
    	//根据carmodelid查询
    	Example example = new Example(CarModelOthername.class);
    	Criteria createCriteria = example.createCriteria();
    	createCriteria.andEqualTo("carmodelid", carModelSave.getCarmodelid());
    	List<CarModelOthername> carModelOthernameList = carModelOthernameDao.selectByExample(example);
    	
    	//查询不到的话就插入一条数据
    	if(carModelOthernameList.size() == 0){
    		CarModelOthername carModelOthername = new CarModelOthername();
    		carModelOthername.setCarmodelid(carModelSave.getCarmodelid());
    		carModelOthername.setOthername(otherName);
    		carModelOthernameDao.insert(carModelOthername);
    	}
    	//查询的到更新数据
    	if(carModelOthernameList.size() == 1){
    		CarModelOthername carModelOthername = carModelOthernameList.get(0);
    		carModelOthername.setOthername(otherName);
    		carModelOthernameDao.updateByPrimaryKey(carModelOthername);
    	}
    }
    
    @Override
    public void insertData(CarModel carModelSave, String otherName) {
    	dao.insert(carModelSave);
    	
    	CarModelOthername carModelOthername = new CarModelOthername();
		carModelOthername.setCarmodelid(carModelSave.getCarmodelid());
		carModelOthername.setOthername(otherName);
		carModelOthernameDao.insert(carModelOthername);
    }
}
